package com.sgb.kjwl.view.ui.activity.manage.special

import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivitySpecialMemberBinding
import com.sgb.kjwl.di.self.DaggerSelfComponent
import com.sgb.kjwl.di.self.SelfModule
import com.sgb.kjwl.view.ui.fragment.manage.OtherInquiryFragment
import com.sgb.kjwl.view.ui.fragment.manage.special.SpecialMemberFragment
import com.sgb.kjwl.viewmodel.manage.CompanySpecialViewModel
import com.sgb.link.utils.SizeUtils
import com.swgk.core.base.di.AppComponent
import com.swgk.core.util.PickSelectEntity
import javax.inject.Inject

/**
 * 企业专项 成员受理
 */
class SpecialMemberActivity : AppActivity() {

    private lateinit var mBinding: ActivitySpecialMemberBinding

    @Inject
    lateinit var mViewModel: CompanySpecialViewModel

    private lateinit var mAdapter: SpecialMemberPageAdapter
    private var mIsFilter = false

    companion object {
        const val REQUEST_CODE_ACCEPTANCE = 1002000
        const val KEY_IS_FILTER = "key_is_filter"
        fun launch(context: Context) {
            launch(context, true)
        }

        /**
         * 个人中心代办区域跳转
         */
        fun launch(context: Context, isFilter: Boolean = false) {
            val intent = Intent(context, SpecialMemberActivity::class.java)
            intent.putExtra(KEY_IS_FILTER, isFilter)
            context.startActivity(intent)
        }
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        super.setupActivityComponent(appComponent)
        DaggerSelfComponent.builder().appComponent(appComponent)
            .selfModule(SelfModule()).build().inject(this)
    }

    override fun initView() {
        super.initView()
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_special_member)
        if (intent != null) {
            mIsFilter = intent.getBooleanExtra(KEY_IS_FILTER, false)
        }
        mViewModel.titles.observe(this, {
            if (it != null && it.isNotEmpty()) {
                initTabLayout(it)
            }
        })
        mBinding.tablayout.visibility = if (mIsFilter) View.VISIBLE else View.GONE
        mBinding.include.titleView.text = "成员受理"
        mBinding.include.ivBack.setOnClickListener { finish() }
        val lp = mBinding.viewPage.layoutParams as LinearLayout.LayoutParams
        lp.topMargin = SizeUtils.dp2px(if (mIsFilter) 10f else 0f)
        mBinding.viewPage.layoutParams = lp
    }

    private fun initTabLayout(titles: List<PickSelectEntity<Any>>) {
        mAdapter = SpecialMemberPageAdapter(supportFragmentManager, titles)
        mBinding.viewPage.adapter = mAdapter
        mBinding.viewPage.offscreenPageLimit = titles.size
        mBinding.tablayout.setupWithViewPager(mBinding.viewPage)
        mBinding.tablayout.tabMode = TabLayout.MODE_FIXED
        mBinding.viewPage.currentItem = 0
    }

    override fun init() {
        super.init()
        if (mIsFilter) {
            mViewModel.getTitle()
        } else {
            mViewModel.getFilterTitle()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_ACCEPTANCE && resultCode == RESULT_OK) {
            val fragmentOne = supportFragmentManager.findFragmentByTag(
                "android:switcher:" + mBinding.viewPage.id.toString() + ":" + 0
            )
            if (fragmentOne != null && fragmentOne is SpecialMemberFragment) {
                fragmentOne.refresh()
            }
            val fragmentTwo = supportFragmentManager.findFragmentByTag(
                "android:switcher:" + mBinding.viewPage.id.toString() + ":" + 1
            )
            if (fragmentTwo != null && fragmentTwo is SpecialMemberFragment) {
                fragmentTwo.refresh()
            }
            val fragmentThree = supportFragmentManager.findFragmentByTag(
                "android:switcher:" + mBinding.viewPage.id.toString() + ":" + 2
            )
            if (fragmentThree != null && fragmentThree is SpecialMemberFragment) {
                fragmentThree.refresh()
            }
            val fragmentFour = supportFragmentManager.findFragmentByTag(
                "android:switcher:" + mBinding.viewPage.id.toString() + ":" + 3
            )
            if (fragmentFour != null && fragmentFour is SpecialMemberFragment) {
                fragmentFour.refresh()
            }
        }
    }


    class SpecialMemberPageAdapter(fm: FragmentManager, titles: List<PickSelectEntity<Any>>) :
        FragmentPagerAdapter(fm) {
        private var mTitles: List<PickSelectEntity<Any>>? = null

        init {
            this.mTitles = titles
        }

        override fun getCount() = mTitles?.size ?: 0

        override fun getItem(position: Int): Fragment {
            if (mTitles == null) return OtherInquiryFragment.newInstance()
            return SpecialMemberFragment.newInstance(mTitles!![position].no)
        }

        override fun getPageTitle(position: Int): CharSequence {
            if (mTitles != null) {
                return mTitles!![position].projectscale
            }
            return ""
        }


    }
}